package com.hucheng.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @Author: 胡成
 * @Version: 0.0.1V
 * @Date: 2019-06-10
 * @Description: 验证工具类
 **/
public class ValidatorUtils {

    /**
     * 正则表达式：验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$";

    /**
     * 正则表达式：验证密码
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";

    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";

    /**
     * 正则表达式：验证邮箱
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    /**
     * 正则表达式：验证汉字
     */
    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";

    /**
     * 正则表达式：验证身份证
     */
    public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";

    /**
     * 正则表达式：验证URL
     */
    public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";

    /**
     * 正则表达式：验证IP地址
     */
    public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";

    /**
     * 校验用户名
     *
     * @param username
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUsername(String username) {
        return Pattern.matches(REGEX_USERNAME, username);
    }

    /**
     * 校验密码
     *
     * @param password
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isPassword(String password) {
        return Pattern.matches(REGEX_PASSWORD, password);
    }

//    /**
//     * 校验手机号
//     *
//     * @param mobile
//     * @return 校验通过返回true，否则返回false
//     */
//    public static boolean isMobile(String mobile) {
//        return Pattern.matches(REGEX_MOBILE, mobile);
//    }

//    /**
//     * 校验邮箱
//     *
//     * @param email
//     * @return 校验通过返回true，否则返回false
//     */
//    public static boolean isEmail(String email) {
//        return Pattern.matches(REGEX_EMAIL, email);
//    }

    /**
     * 校验汉字
     *
     * @param chinese
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isChinese(String chinese) {
        return Pattern.matches(REGEX_CHINESE, chinese);
    }

    /**
     * 校验身份证
     *
     * @param idCard
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isIDCard(String idCard) {
        return Pattern.matches(REGEX_ID_CARD, idCard);
    }

    /**
     * 校验URL
     *
     * @param url
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUrl(String url) {
        return Pattern.matches(REGEX_URL, url);
    }

    /**
     * 校验IP地址
     *
     * @param ipAddr
     * @return
     */
    public static boolean isIPAddr(String ipAddr) {
        return Pattern.matches(REGEX_IP_ADDR, ipAddr);
    }


    public static boolean isEmpty(String str) {

        return ((str == null) || (str.trim().length() == 0));
    }

    public static boolean isNotEmpty(String str) {
        if(str==null){
            return false;
        }

        return ((str != null) && (str.trim().length() > 0)&&!"null".equals(str.trim()));
    }

    public static boolean isNotEmpty(@SuppressWarnings("rawtypes") Map result){
        if(result!=null&&!result.isEmpty()&&result.size()>0){
            return true;
        }
        return false;
    }

    public static boolean isEmpty(@SuppressWarnings("rawtypes") Map result){
        return !isNotEmpty(result);
    }

    public static boolean isNotEmpty(@SuppressWarnings("rawtypes") List result){
        if(result!=null&&!result.isEmpty()&&result.size()>0){
            return true;
        }
        return false;
    }

    public static boolean isEmpty(@SuppressWarnings("rawtypes") List result){
        return !isNotEmpty(result);
    }

    public static boolean isNotEmpty(@SuppressWarnings("rawtypes") Set result){
        if(result!=null&&!result.isEmpty()&&result.size()>0){
            return true;
        }
        return false;
    }

    public static boolean isEmpty(@SuppressWarnings("rawtypes") Set result){
        return !isNotEmpty(result);
    }

    public static boolean isNotNull(String str) {
        if (str == null) {
            return false;
        }
        return ((str != null) && (str.trim().length() > 0) && !"null"
                .equals(str.trim()));
    }

    public static boolean isNotNull(Object obj){
        if(obj==null){
            return false;
        }
        return isNotNull(obj+"");
    }

    public static boolean isNull(Object obj){
        return !isNotNull(obj);
    }

    public static boolean isNull(String str) {
        return !isNotNull(str);
    }

    public static boolean isNotNull(Integer str) {
        if (str == null) {
            return false;
        }
        return true;
    }

    public static boolean isNull(Integer str) {
        return !isNotNull(str);
    }

    public static boolean isNotNull(Long str) {
        if (str == null) {
            return false;
        }
        return true;
    }

    public static boolean isNull(Long str) {
        return !isNotNull(str);
    }

    public static boolean isNotNull(Date str) {
        if (str == null) {
            return false;
        }
        return true;
    }

    public static boolean isNull(Date str) {
        return !isNotNull(str);
    }


    public static boolean isBlank(String str) {

        int length = 0;

        if ((str == null) || ((length = str.length()) == 0)) {
            return true;
        }

        for (int i = 0; i < length; i++) {
            if (Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }

        return false;
    }


    public static boolean isLong(String v){
        if(!isDigital(v)){
            return false;
        }
        if(Long.valueOf(v)<=0){
            return false;
        }
        return true;
    }

    public static boolean isTimeStamp(String timeStamp) {
        if(!ValidatorUtils.isLong(timeStamp)){
            // 空 小于0
            return false;
        }
        // 1973-03-03 17:46:40.000000改时间前9位
        if(timeStamp.length()<10){
            return false;
        }
        return true;
    }

    public static boolean checkString(String str, String regex) {

        return str.matches(regex);
    }

    public static boolean isMd5(String md5) {

        if (md5.length() != 32) {
            return false;
        }

        return checkString(md5, "[0-9A-Fa-f]+");
    }
    public static boolean validTime(String time, String pattern) {

        if (time.length() != 14) {
            return false;
        }

        if (!checkString(time, "[2][0-9]+")) {
            return false;
        }

        DateFormat formatter = new SimpleDateFormat(pattern, Locale.ENGLISH);

        Date date = null;
        try {
            date = (Date) formatter.parse(time);
        } catch (ParseException e) {
            return false;
        }
        return time.equals(formatter.format(date));
    }

    public static boolean isInfoCode(String code) {

        code = StringUtils.getInstance().nullToStrTrim(code);
        if(code.length() != 6) {
            return false;
        }

        String regex = "^([1-9])\\d{5}$";

        return code.matches(regex);
    }

    public static boolean isMobile(String code){
        code = StringUtils.getInstance().nullToStrTrim(code);
        if(code.length() != 11) {
            return false;
        }
        String regex = "^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}|17[0-9]{9}$|18[0-9]{9}$";

        return code.matches(regex);
    }
    public static boolean isEmail(String code){
        code = StringUtils.getInstance().nullToStrTrim(code);
        String regex = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$";
        return code.matches(regex);
    }

    public static boolean isURL(String value){
        String regex = "^(http|https)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&amp;%\\$\\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\\:[0-9]+)*(/($|[a-zA-Z0-9\\.\\,\\?\\'\\\\\\+&amp;%\\$#\\=~_\\-]+))*$";
        return value.matches(regex);
    }

    public static boolean isDigital(String v){
        v = StringUtils.getInstance().nullToStrTrim(v);
        if(v.length()==0){
            return false;
        }
        String regex = "^[0-9]*$";
        return v.matches(regex);
    }

    public static boolean isDouble(String v){
        v = StringUtils.getInstance().nullToStrTrim(v);
        if(v.length()==0){
            return false;
        }
        Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
        return pattern.matcher(v).matches();
    }

    public static boolean isInteger(String v){
        if(!isDigital(v)){
            return false;
        }
        if(Integer.valueOf(v)<=0){
            return false;
        }
        return true;
    }

    public static boolean isSms(String code){
        code = StringUtils.getInstance().nullToStrTrim(code);
        String regex = "^\\d{6}$";
        return code.matches(regex);
    }


    public static boolean isMoney(String value){
        value = StringUtils.getInstance().nullToStrTrim(value);
        String regex = "^([1-9][\\d]{0,7}|0)(\\.[\\d]{1,2})?$";
        return value.matches(regex);
    }


}
